/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import co.edu.unal.satacredito.database.connection.AdministratorDatastoreFactory;
import co.edu.unal.satacredito.database.dao.AdministratorMongoDao;
import co.edu.unal.satacredito.database.entity.Administrator;
import java.net.UnknownHostException;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author migueldiaz
 */
@ManagedBean
@RequestScoped
public class loginBean {

    private String user;
    private String password;
    private AdministratorMongoDao admDao;

  

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String consultar() throws UnknownHostException, InterruptedException {
       
        this.admDao = new AdministratorMongoDao(AdministratorDatastoreFactory.createDatastore());
        
        try {
            Administrator adm=admDao.findByUser(user);
            if (adm.getPassword().equals(password)) {
                return "main";
            } else {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Usuario o Contraseña no validas"));
                return null;
            }
            
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Usuario o Contraseña no validas"));
                return null;
        }

    }
}
